package com.macro.mall.tiny.controller;

import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.mbg.model.UmsMenu;
import com.macro.mall.tiny.mbg.model.UmsResource;
import com.macro.mall.tiny.mbg.model.UmsRole;
import com.macro.mall.tiny.service.UmsRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/role")
@Api(tags="UmsRoleController")
@Tag(name="UmsRoleController",description = "后台用户管理")
@PreAuthorize("@ex.hasAuthority('/role')")
public class UmsRoleController {

    @Autowired
    private UmsRoleService umsRoleService;

    //添加角色
    @PostMapping("/create")
    @ApiOperation("添加角色")
    public CommonResult create(@RequestBody UmsRole umsRole){
        log.info("添加角色:{}",umsRole);

        Integer code=umsRoleService.create(umsRole);
        return code==1? CommonResult.success(code):CommonResult.failed();
    }

    //修改角色信息
    @PostMapping("/update/{id}")
    @ApiOperation("修改角色信息")
    public CommonResult updateById(@PathVariable Long id,@RequestBody UmsRole umsRole){
        log.info("修改用户信息：{}，{}",id,umsRole);

        Integer code=umsRoleService.updateById(id,umsRole);
        return code==1? CommonResult.success(code):CommonResult.failed();
    }

    //批量删除角色
    @PostMapping("/delete")
    @ApiOperation("批量删除用户角色信息")
    public CommonResult delete(@RequestParam(value="ids")List<Long> ids){
        log.info("批量删除用户角色信息：{}",ids);

        Integer code=umsRoleService.delete(ids);
        return code>0?CommonResult.success(code):CommonResult.failed();
    }

    //获取所有角色信息
    @GetMapping("/listAll")
    @ApiOperation("获取所有角色信息")
    public CommonResult<List<UmsRole>> getListAll(){
        log.info("获取所有角色信息");

        List<UmsRole> umsRoleList=umsRoleService.getListAll();
        return CommonResult.success(umsRoleList);
    }

    //根据角色名称分页获取角色信息
    @GetMapping("/list")
    @ApiOperation("根据角色名称分页获取角色信息")
    public CommonResult<CommonPage<UmsRole>> getPageList(
            @RequestParam(value="keyword",required=false)String keyword,
            @RequestParam(value="pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value="pageSize",defaultValue = "5")Integer pageSize){
        log.info("根据角色名称分页获取角色信息:{},{},{}",keyword,pageNum,pageSize);

        List<UmsRole> umsRoleList=umsRoleService.getPageList(keyword,pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(umsRoleList));
    }

    //修改角色状态
    @PostMapping("/updateStatus/{id}")
    @ApiOperation("修改角色状态")
    public CommonResult updateStatus(@RequestParam(value="id")Long id,@RequestParam(value="status")Integer status){
        log.info("修改角色状态:{},{}",id,status);

        Integer code=umsRoleService.updateStatus(id,status);
        return code==1?CommonResult.success(code):CommonResult.failed();
    }

    //获取角色相关菜单
    @GetMapping("/listMenu/{roleId}")
    @ApiOperation("获取角色相关菜单")
    public CommonResult<List<UmsMenu>> getListMenu(@PathVariable Long roleId){
        log.info("获取角色相关菜单:{}",roleId);

        List<UmsMenu> umsMenuList=umsRoleService.getListMenu(roleId);
        return CommonResult.success(umsMenuList);
    }

    //获取角色相关资源
    @GetMapping("/listResource/{roleId}")
    @ApiOperation("获取角色相关资源")
    public CommonResult<List<UmsResource>> getListResource(@PathVariable Long roleId){
        log.info("获取角色相关资源:{}",roleId);

        List<UmsResource> resourceList=umsRoleService.getListResource(roleId);
        return CommonResult.success(resourceList);
    }

    //给角色分配菜单
    @PostMapping("/allocMenu")
    @ApiOperation("给角色分配菜单")
    public CommonResult allocMenu(@RequestParam(value="roleId")Long roleId,
                                  @RequestParam(value="menuIds")List<Long> menuIds){
        log.info("给角色分类菜单：{}，{}",roleId,menuIds);

        Integer code=umsRoleService.allocMenu(roleId,menuIds);
        return code>0?CommonResult.success(code):CommonResult.failed();
    }

    //给角色分配资源
    @PostMapping("/allocResource")
    @ApiOperation("给角色分配资源")
    public CommonResult allocResource(@RequestParam(value="roleId")Long roleId,
                                      @RequestParam(value="resourceIds")List<Long> resourceIds){
        log.info("给角色分配资源:{},{}",roleId,resourceIds);

        Integer code=umsRoleService.allocResource(roleId,resourceIds);
        return code>0?CommonResult.success(code):CommonResult.failed();
    }

}
